package com.itheima.health.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.POJO.CheckGroup;
import com.itheima.health.POJO.CheckItem;
import com.itheima.health.common.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//日志
@Slf4j
//controller组件，响应json
@RestController
@RequestMapping("/checkgroup/")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;


    /**
     * @description 新增检查组
     * @date 2021/11/23 14:14
     * @param checkGroup:
     * @return com.itheima.health.entity.Result
     */
    @PostMapping("/add.do")
    public Result addGroup(@RequestBody CheckGroup checkGroup,Long[] checkitemIds){
        //0.参数非空判断
       log.info(checkGroup.toString());
        String code = checkGroup.getCode();
        String name = checkGroup.getName();
        if(code==null || name==null){
            return new Result(false,"请填写编码和名字",null);
        }

        LambdaQueryWrapper<CheckGroup> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(CheckGroup::getCode,code).or().eq(CheckGroup::getName,name);
        CheckGroup one = checkGroupService.getOne(wrapper);
        //编码相同，返回错误
        if(one!=null){
            return new Result(false,"存在相同的编码或名字",one);
        }

        //增加组
        checkGroupService.save(checkGroup);
        //查询组获取id
        LambdaQueryWrapper<CheckGroup> wapper=new LambdaQueryWrapper<>();
        wapper.eq(CheckGroup::getCode,code).eq(CheckGroup::getName,name);
        CheckGroup group = checkGroupService.getOne(wapper);
        Integer id=group.getId();
        //获取新组中items，加入中间表
        checkGroupService.addItems(id,checkitemIds);
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS,checkGroup);
    }

    /**
     * @description 分页查询
     * @date 2021/11/24 14:59
     * @param pageBean:
     * @return com.itheima.health.entity.PageResult
     */
    @PostMapping("/findPage.do")
    public PageResult findByPage(@RequestBody QueryPageBean pageBean) {
        //0.加入分页查询插件
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //2.判断页码和每页记录数要大于0
        if(currentPage<=0 || pageSize<=0){
            return null;
        }
        //plus分页
        IPage<CheckGroup> pageInfo=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<CheckGroup> wrapper=new LambdaQueryWrapper<>();
        if(queryString!=null) {
            wrapper.like(CheckGroup::getCode, queryString).or().like(CheckGroup::getName, queryString);
        }
        IPage<CheckGroup> page = checkGroupService.page(pageInfo, wrapper);
        return new PageResult(page.getTotal(), page.getRecords());
    }

    /**
     * @description 编辑检查组
     * @date 2021/11/23 17:17
     * @param checkGroup:
     * @return com.itheima.health.entity.Result
     */
    @PostMapping("/edit.do")
    public Result updateCheckItem(@RequestBody CheckGroup checkGroup,Long[] checkitemIds){
        //1.修改中间表中的内容，调用service方法
        Result result= checkGroupService.modifyItems(checkGroup,checkitemIds);
        return result;
    }

    /**
     * @description 根据id查询检查组
     * @date 2021/11/23 17:37
     * @param id:
     * @return com.itheima.health.entity.Result
     */
    @GetMapping("/findById.do")
    public Result findById(Integer id){
        LambdaQueryWrapper<CheckGroup> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(CheckGroup::getId,id);
        CheckGroup one = checkGroupService.getOne(wrapper);
        if(one==null){
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,one);
    }

    /**
     * @description 根据检查组ID查询检查组包含的多个检查项ID
     * @date 2021/11/27 21:04
     * @param id:
     * @return com.itheima.health.entity.Result
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId.do")
    public Result findCheckItemIds(Integer id){
        Result result= checkGroupService.findItems(id);
        return result;
    }
}
